package com.youuvs.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.youuvs.dto.BuyTicketDto;
import com.youuvs.dto.GetUserByMovieIdDto;
import com.youuvs.service.IUserService;
import com.youuvs.domain.User;
import com.youuvs.query.UserQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.youuvs.result.JSONResult;
import com.youuvs.result.PageList;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JSONResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }

    /**
    *根据Id查询电影票信息，需要用户服务通过OpenFeign调用电影服务
    */
    @RequestMapping(value = "/getTicketByUserId/{id}",method = RequestMethod.GET)
    public JSONResult getTicketByUserId(@PathVariable("id") Long id){
        userService.getTicketByUserId(id);
        return new JSONResult();
    }


    /**
     *买票
     */
    @RequestMapping(value = "/buyTicket",method = RequestMethod.POST)
    public JSONResult buyTicket(@RequestBody BuyTicketDto dto){
        userService.buyTicket(dto);
        return new JSONResult();
    }

    /**
     * 根据电影Id，查询用户是否拥有此电影票，电影票是否已经被使用，电影票是否过期，过期提示不可观看
     */
    @RequestMapping(value = "/getUserByMovidId",method = RequestMethod.POST)
    public JSONResult getUserByMovidId(@RequestBody GetUserByMovieIdDto dto){
        userService.getTicketByMovieId(dto);

    return JSONResult.success("拥有,未被使用,未过期");
    }

    @RequestMapping(value="/insAndPresent",method= RequestMethod.POST)
    public JSONResult insAndPresent(@RequestBody User user){
        userService.insAndPresent(user);
        return new JSONResult();
    }



}
